#include "faultinfodialog.h"
#include "ui_faultinfodialog.h"
#include "searchbinfile.h"
#include "serialport.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QVector>
#include "readsettingfile.h"
#include <QTextCodec>
#include <QTime>
#include <QDebug>
#include <QTableWidgetItem>
#include <QScrollBar>
#include "progessdialog.h"
#include "ifuncwin.h"
#include <qlabel.h>
#include "globalfunc.h"
#include "kmessagebox.h"
#include "recorderprint.h"

//ProgessDialog progessdialog;
FaultInfoDialog::FaultInfoDialog(QWidget *parent) ://IFuncWin    QWidget  QDialog
    QDialog(parent),
    ui(new Ui::FaultInfoDialog)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);//隐藏标题设置为顶层显示
    //m_winId = "FaultInfoDialog";
    tc =  QTextCodec::codecForName("UTF-8");
    tableWidget_creat();
    timer = new QTimer;
    connect(timer,SIGNAL(timeout()),this,SLOT(UpdateProgess()),Qt::DirectConnection);


//    reagentInfodialog = new ReagentInfoDialog;
//    connect(this,SIGNAL(sendreagetsamNum(QString)),reagentInfodialog,SLOT(getfaultID(QString)));
//    connect(this,SIGNAL(sendtimeerrtorg()),reagentInfodialog,SLOT(rsvtimeerr()));
//    connect(reagentInfodialog,SIGNAL(sendtofalutthiscolse(bool)),this,SLOT(reagentcolseflg(bool)));//by kss20161201
//    reagentInfodialog->hide();

//    logindialog = new LogInDialog;
//    //logindialog->start_process("fault");
//    logindialog->hide();

    ui->progressBar->setValue(0);



    timerreadmicroswitch = new QTimer;
    timerreadmicroswitch->stop();
    connect(timerreadmicroswitch,SIGNAL(timeout()),this,SLOT(Faultdealsyms()));

    timermicroswitchdeal = new QTimer;
    timermicroswitchdeal->stop();
    connect(timermicroswitchdeal,SIGNAL(timeout()),this,SLOT(ifuncreadmicroswitch()));

    ui->Btn_clearFault->setStyleSheet(stylesheet_allbutton);
    ui->Btn_faultDelete->setStyleSheet(stylesheet_allbutton);
    ui->Btn_faultInfoClose->setStyleSheet(stylesheet_allbutton);
    serialport = main_control_data.serialport;


}
void FaultInfoDialog::Faultsccussdeal_save_journal(int dealsccussNum)
{
        QSqlQuery query;
        QString  faultCode;
        QString  faultDescrip;
        FalutSummaInfo = tr("故障消除");

        query.prepare("select * from FaultinfTable where dealnum = :dealNum");
        query.bindValue(":dealNum",dealsccussNum);
        query.exec();

        QSqlRecord rec = query.record();
        while(query.next())
        {
            rec = query.record();
            faultCode =(rec.value( rec.indexOf("Faultcode") ).toString());
            faultDescrip = (rec.value( rec.indexOf("DailyFaultdescription") ).toString());
            FalutinfoFunc = faultCode +":"+faultDescrip;
            save_journal_info(FalutSummaInfo,FalutinfoFunc,1);
        }
        query.prepare(QObject::tr("delete from FaultinfTable where dealnum = :dealNum"));
        query.bindValue(":dealNum",dealsccussNum);
        query.exec();
}
void FaultInfoDialog::Faultdealsyms()
{
    bool ok;
    timerreadmicroswitch->stop();
    //qDebug() <<"Faultdealsyms come ";
    qDebug()<<"allowMaintenanceExe4 = "<<allowMaintenanceExe<<oldfaultallowMaintenanceExe;
    if(this->isHidden() == true)
    {
        return;
    }
    if(!isSystemSleep)
    {
        timermicroswitchdeal->stop();
    }
    if(Btclearfaultstep == 0xFFFF)
    {
        Btclearfaultstep = 0;
        tableWidget_creat();
        return;
    }
   else if(Btclearfaultstep == 1)
   {
       if(communcheckflg)
       {
           Faultsccussdeal_save_journal(0);
           Communcheckrepeat = 0x66;
           clearFault_step();
       }
       else
       {
               Communcheckrepeat = 0xFF;
               Faultto_clearFault();
       }
    }
   else if(Btclearfaultstep == 2)
   {
       if(Ysamreturnflg)
       {
           Faultsccussdeal_save_journal(1);
           YsamErrrepeat =0x66;
           //qDebug() <<"YsamErrrepeat  0x66 ";
       }
       else
       {
           YsamErrrepeat =0xff;
           Ysamreturnflg = false;
           //qDebug() <<"YsamErrrepeat  0x66 ";
       }
       clearFault_step();
  }
  else if(Btclearfaultstep == 3)
   {
       if(Xsamreturnflg)
       {
           Faultsccussdeal_save_journal(2);
           XsamErrrepeat =0x66;
           //qDebug() <<"XsamErrrepeat  0x66 ";
       }
       else
       {
           XsamErrrepeat =0xff;
           //qDebug() <<"XsamErrrepeat  0xff ";
       }
       clearFault_step();
   }
   else if(Btclearfaultstep == 4)
   {
       if(Dilreturnflg)
       {
           Faultsccussdeal_save_journal(3);
           DilErrrepeat =0x66;
       }
       else
       {
           DilErrrepeat =0xff;
       }
       clearFault_step();
   }
   else if(Btclearfaultstep == 5)
   {
       if(Diffreturnflg)
       {
           Faultsccussdeal_save_journal(4);
           DiffErrrepeat =0x66;
       }
       else
       {
           DiffErrrepeat =0xff;
       }
       clearFault_step();
   }
   else if(Btclearfaultstep == 6)
   {
//       tableWidget_creat();
       Lhreagentreturnflg =true;
//       for(int i = 0;i<faultdealorder.count();i++)
//       {
//           if(faultdealorder.at(i).toInt(&ok,10) == 5)//LH
//           {
//                 Lhreagentreturnflg = false;
//           }
//      }
       if(Lhreagentreturnflg)
       {
           Lhreagentrepeat =0x66;
       }
       else
       {
           Lhreagentrepeat =0xff;
       }
        clearFault_step_familytwo();
   }
   else if(Btclearfaultstep == 7)
   {
       Diffreagentreturnflg =true;
       if(Diffreagentreturnflg)
       {
           Diffreagentrepeat =0x66;
       }
       else
       {
           Diffreagentrepeat =0xff;
       }
        clearFault_step_familytwo();
   }
   else if(Btclearfaultstep == 8)
   {
       Dilreagentreturnflg =true;
       if(Dilreagentreturnflg)
       {
           Dilreagentrepeat =0x66;
       }
       else
       {
           Dilreagentrepeat =0xff;
       }
        clearFault_step_familytwo();
   }

   else if(Btclearfaultstep == 9)
   {
       if(fluidreturnflg)
       {
           Faultsccussdeal_save_journal(23);
           fluidErrrepeat =0x66;
       }
       else
       {
           fluidErrrepeat =0xff;
       }
        clearFault_step_familytwo();
   }
    else  if(Btclearfaultstep == 10)
    {
       Btclearfaultstep = 0;
       clearFault_step();
    }
    else if(Btclearfaultstep == 50)
    {
        Btclearfaultstep = 0;
        negativeErrrepeat = 0x66;
        Faultsccussdeal_save_journal(9);
        clearFault_step_familytwo();
    }
    else if(Btclearfaultstep == 101)
    {
        SearchBinFile searchfile;
        QString str;
        int fd;
        fd = serialport->getmfd();
        int times = 0;
       Btclearfaultstep = 102;
       //clearFault_step_familytwo();
       ui->Btn_clearFault->setEnabled(false);
       ui->Btn_faultDelete->setEnabled(false);
       ui->Btn_faultInfoClose->setEnabled(false);
       if(run_Ftime_check("F018"))
       {
              SystemSleepexitfalut =true;
              Faultto_clearFault();
               return;
       }
       searchfile.search("F018",str,times);
       int funcTime = GetFuncTime(str);
       serialport->sendData(str,fd,18,times);
       timerreadmicroswitch->stop();
       progressBarinit(funcTime);
    }
    else if(Btclearfaultstep == 102)
    {
        SearchBinFile searchfile;
        int fd;
        QString str;
        fd = serialport->getmfd();
        int times = 0;

       Btclearfaultstep = 103;
       //clearFault_step_familytwo();
       ui->Btn_clearFault->setEnabled(false);
       ui->Btn_faultDelete->setEnabled(false);
       ui->Btn_faultInfoClose->setEnabled(false);
       QString funcExitSleep;
       if(main_control_data.SystemSleepTime <= 600)
       {
           funcExitSleep = "F019";
       }
       else if(main_control_data.SystemSleepTime > 600 && main_control_data.SystemSleepTime <= 3600)
       {
           funcExitSleep = "F020";
       }
       else if(main_control_data.SystemSleepTime > 3600)
       {
           funcExitSleep = "F021";
       }
       if(run_Ftime_check(funcExitSleep))
       {
            SystemSleepexitfalut =true;
              Faultto_clearFault();
               return;
       }
       searchfile.search(funcExitSleep,str,times);
       int funcTime = GetFuncTime(str);
       //qDebug() << "funcExitSleep = " << funcExitSleep;
       int ismaintest;
       if(funcExitSleep.indexOf("F") > -1)
       {
           ismaintest = funcExitSleep.mid(1,3).toInt();
       }

       if(funcExitSleep.indexOf("t") > -1)
       {
           ismaintest = funcExitSleep.mid(4,3).toInt();
       }
       serialport->sendData(str,fd,ismaintest,times);
       timerreadmicroswitch->stop();
       progressBarinit(funcTime);
    }
    else if(Btclearfaultstep == 103)
    {
       Btclearfaultstep = 0;
       if(SystemSleepexitfalut)
       {
          Faultto_clearFault();
       }
       else
       {
           Faultsccussdeal_save_journal(24);
           SystemSleepexitfalut =false;
           main_control_data.SystemSleepTime = 0;
           clearFault_step_familytwo();
       }
    }
    else if(Btclearfaultstep == 300)
    {
        if(global_pressurewarnrpeflg == 0xFF)
        {
            SearchBinFile searchfile;
            QString str;
            int times = 0;
            int fd;
            fd = serialport->getmfd();

            global_pressurewarnrpeflg = 0xFF;
            if(!searchfile.search("F036",str,times))
            {
                   //SystemSleepexitfalut =true;
                   global_errdealflg = true;
                   Faultto_clearFault();
                   return;
            }
            global_errdealflg = true;
            int funcTime = GetFuncTime(str);
            int sendlength = serialport->sendData(str,fd,36,times);
            //qDebug() << sendlength;

            progressBarinit(funcTime);
            Btclearfaultstep = 301;
            timerreadmicroswitch->stop();
        }
        else
        {
            global_pressurewarnrpeflg = 0;
            global_errdealflg = true;
            Faultto_clearFault();
            return;
//            Btclearfaultstep = 0;
//            hydraulicpressurerepeat = 0x66;
//            Faultsccussdeal_save_journal(10);
//            clearFault_step_familythree();
//            global_errdealflg = false;
        }
    }
    else if(Btclearfaultstep == 301)
    {
        Btclearfaultstep = 0;
        hydraulicpressurerepeat = 0x66;
        Faultsccussdeal_save_journal(10);
        faultNum = get_faultInfo_from_db();
        if(faultNum == 0)
        {
          clearFault_step_familythree();
          global_errdealflg = false;
        }
        else if(faultNum > 0)
        {
           clearFault_step();
        }
    }
    else if(Btclearfaultstep == 380)
    {
       tableWidget_creat();
       Diltempalarmrepeat  =1;
//       for(int i = 0;i<faultdealorder.count();i++)
//       {
//           if(faultdealorder.at(i).toInt(&ok,10) == 11)//DIL
//           {
//                Diltempalarmrepeat  =false;
//           }
//      }
       if(global_faultcode.count()>0)
       {
           //if(global_faultcode.at(0) == "638")
           Diltempalarmrepeat  =0;
       }
       if(Diltempalarmrepeat)
       {
           Faultsccussdeal_save_journal(11);
           Diltempalarmrepeat = 0x66;
       }
       else
       {
           Diltempalarmrepeat = 0xff;
       }
       clearFault_step_familyfour();
    }
    else if(Btclearfaultstep == 381)
    {
        tableWidget_creat();
        Difftempalarmrepeat  =1;
        if(global_faultcode.count()>0)
        {
            Difftempalarmrepeat  =0;
        }
        if(Difftempalarmrepeat)
        {
            Faultsccussdeal_save_journal(12);
            Difftempalarmrepeat = 0x66;
        }
        else
        {
            Difftempalarmrepeat = 0xff;
        }
        clearFault_step_familyfour();
    }
    else if(Btclearfaultstep == 382)
    {
        if(hardware_V_check(9))
        {
            Hgbalarmrepeat = 0x66;
            Faultsccussdeal_save_journal(33);
        }
        else
        {
            Hgbalarmrepeat = 0xff;
        }
        Vhalarmrepeat  = 0;//该类电压故障不用停机
        tableWidget_creat();
        clearFault_step_familyfour();
    }
    else if(Btclearfaultstep == 400)
    {
        SearchBinFile searchfile;
        QString str2;
        int fd;
        fd = serialport->getmfd();
        int times = 0;
        if(!searchfile.search("F029",str2,times))
        {
               //SystemSleepexitfalut =true;
               Faultto_clearFault();
                return;
        }
        //searchfile.search("F029",str2,times);
        serialport->sendData(str2,fd,29,times);
        int funcTime = GetFuncTime(str2);
        progressBarinit(funcTime);
        Btclearfaultstep = 601;
        //Faultto_clearFault();
    }
    else if(Btclearfaultstep == 601)
    {
        //Faultto_clearFault();
        tableWidget_creat();
        clearFault_step_familyfour();
    }
    else if(Btclearfaultstep == 620)
    {
         Btclearfaultstep = 601;
         Faultto_clearFault();
    }
}

FaultInfoDialog::~FaultInfoDialog()
{
    delete ui;
}
void FaultInfoDialog::tableWidget_creat()
{
    int i = 0, j = 0;
    listingrow = 0;
    selectedIndex =101 ;

//    ui->progressBar->setValue(0);
    ui->faultInfo_textEdit->setText("");
//    timeisover = true;//时序是否正常完成标志
//    isinitimessuccess = true;//初始化时序完成
//    iscountover = true; //运算完成
//    signaltimes = true;//单帧命令完成
    ErrClearBtnflg =false;
    Clear_RerrNum.clear();

    faultNum = get_faultInfo_from_db();
    listingrow = ui->tableWidget_faultInfo->verticalScrollBar()->value();
    listingrow = 0;
    ui->tableWidget_faultInfo->verticalScrollBar()->setSliderPosition(listingrow);
    set_btn_state();

    ui->tableWidget_faultInfo->setColumnCount(3);
    ui->tableWidget_faultInfo->setRowCount(100);
    ui->tableWidget_faultInfo->setFrameShape(QFrame::NoFrame);
    ui->tableWidget_faultInfo->setEditTriggers(QAbstractItemView::NoEditTriggers);//使其不可编辑
    ui->tableWidget_faultInfo->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget_faultInfo->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget_faultInfo->verticalHeader()->hide();
    ui->tableWidget_faultInfo->verticalScrollBar()->installEventFilter(this);
    ui->tableWidget_faultInfo->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

//    ui->tableWidget_faultInfo->horizontalHeader()->setVisible(true);
    ui->tableWidget_faultInfo->horizontalHeader()->setClickable(false);
//    ui->tableWidget_faultInfo->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
//    ui->tableWidget_faultInfo->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度






    for( i = 0; i < 100; i++)
    {
        for( j = 0; j < 3; j++)
        {
            ui->tableWidget_faultInfo->setItem( i, j, new QTableWidgetItem("") );
            ui->tableWidget_faultInfo->item(i,j)->setTextAlignment(Qt::AlignCenter);
        }
    }
    for( i = faultNum; i < 100; i++)
    {
        for( j = 1; j < 3; j++)
        {
            ui->tableWidget_faultInfo->item(i,j)->setFlags(Qt::NoItemFlags);//bukexuanzhong
            //ui->tableWidget_faultInfo->item(i,j)->setSelected(false);
        }
    }
    for(int i = 0;i < 100; ++i)
    {
        ui->tableWidget_faultInfo->item(i,0)->setText(QString::number(i+1,10));
        ui->tableWidget_faultInfo->item(i,0)->setBackgroundColor(QColor(102, 204, 153));
        //ui->tableWidget_faultInfo->item(i,0)->setSelected(false);
        ui->tableWidget_faultInfo->item(i,0)->setFlags( Qt::ItemIsSelectable );
        ui->tableWidget_faultInfo->item(i,0)->setFlags( Qt::ItemIsEditable );
    }

    //设置表头内容
    QStringList header;
    header<<""<<tr("故障码")<<tr("故障描述");
    ui->tableWidget_faultInfo->setHorizontalHeaderLabels(header);

    ui->tableWidget_faultInfo->horizontalHeader()->setFixedHeight(30); //设置表头的高度
    //ui->tableWidget_faultInfo->verticalHeader()->setFixedWidth(30);
    ui->tableWidget_faultInfo->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(102, 204, 153);gridline-color: rgb(255, 255, 255);border:1px solid#e1e0e0;}"); //设置表头背景色
    ui->tableWidget_faultInfo->setAutoScroll(false);

    ui->tableWidget_faultInfo->setColumnWidth(0, 30);
    ui->tableWidget_faultInfo->setColumnWidth(1, 105);
    //ui->tableWidget_faultInfo->setColumnWidth(2, 100);
    ui->tableWidget_faultInfo->setColumnWidth(2, 400);

    QString stylesheettableWidget_listing =   //"QTableWidget{background-color:#e2f7eb;}";
                                                "QTableWidget{background-color:#ffffff;"
                                                "alternate-background-color: #e2f7eb;"
                                                "color: rgb(0, 0, 0);"
                                                "selection-color: rgb(255, 255, 255);"
                                                "selection-background-color: rgb(32, 131, 95);"
                                                "}";


    ui->tableWidget_faultInfo->setStyleSheet(stylesheettableWidget_listing);
    ui->tableWidget_faultInfo->setAlternatingRowColors(true);//间隔显示颜色

    for( i = 0; i++; i < 5 )
    {
        ui->tableWidget_faultInfo->setRowHeight( i, 42 );
    }

    connect( ui->tableWidget_faultInfo, SIGNAL(cellPressed(int,int)),this,SLOT(faultInfo_pressedHandle(int,int)));
    fill_tableWidget_faultInfo();
    if(falutflg)
    {
        if(faultNum == 0)
        {
          ui->Btn_clearFault->setEnabled(false);
          ui->Btn_faultDelete->setEnabled(false);
          ui->Btn_faultInfoClose->setEnabled(true);
          ui->Btn_down->setEnabled(false);
          ui->Btn_up->setEnabled(false);
          emit  sendFaultInfoClear(1,false);
          if(!Btclearfaultflg)
          {
//             this->close();
//              allowEnterSleep = oldfaultallowEnterSleep;
//              allowMaintenanceExe = oldfaultallowMaintenanceExe;
//             main_control_data.faultshowflg = false;
          }
        }
        else
        {
            ui->Btn_clearFault->setEnabled(true);
            ui->Btn_faultDelete->setEnabled(true);
           if(faultNum>5)
           {
               ui->Btn_down->setEnabled(true);
               ui->Btn_up->setEnabled(true);
           }
           else
           {
               ui->Btn_down->setEnabled(false);
               ui->Btn_up->setEnabled(false);
           }
        }
    }
    else
    {
        if(faultNum == 0)
        {
          ui->Btn_clearFault->setEnabled(false);
          ui->Btn_faultDelete->setEnabled(false);
          ui->Btn_faultInfoClose->setEnabled(true);
          ui->Btn_down->setEnabled(false);
          ui->Btn_up->setEnabled(false);
          emit  sendFaultInfoClear(1,false);

          //allowEnterSleep = oldfaultallowEnterSleep;
          //allowMaintenanceExe = oldfaultallowMaintenanceExe;
        }
        else
        {
            ui->Btn_clearFault->setEnabled(true);
            ui->Btn_faultDelete->setEnabled(true);
           if(faultNum>5)
           {
               ui->Btn_down->setEnabled(true);
               ui->Btn_up->setEnabled(true);
           }
           else
           {
               ui->Btn_down->setEnabled(false);
               ui->Btn_up->setEnabled(false);
           }
        }
    }
}

int FaultInfoDialog::get_faultInfo_from_db()
{
    int i = 0;
    QString StrlimtNum;
    int limtNum;

    faultCode.clear();
    faultState.clear();
    faultDescrip.clear();
    faultdealorder.clear();
    nowerrNum.clear();
    global_faultInfo.clear();
    global_faultlevel.clear();

    QSqlQuery query;
    query.prepare("select * from FaultinfTable");
    query.exec();

    QSqlRecord rec = query.record();
    int Rcnum =0;
    while(query.next())
    {
        rec = query.record();
        //Rcnum = rec.indexOf("Faultcode");
        ////qDebug()<<"query.next() i= "<<i;
        faultCode.append(rec.value( rec.indexOf("Faultcode") ).toString());
        faultState.append(rec.value( rec.indexOf("solvesum") ).toString());
        faultDescrip.append(rec.value( rec.indexOf("FaultDescription") ).toString());
        global_faultInfo.append(rec.value( rec.indexOf("FaultDescription") ).toString());
        global_faultlevel.append(rec.value( rec.indexOf("State") ).toString());
        faultdealorder.append(rec.value( rec.indexOf("dealnum") ).toString());
        limtNum = rec.value( rec.indexOf("limtevunum") ).toInt();
        if(limtNum<16)
        {
            StrlimtNum = "000";
        }
        else
        {
            StrlimtNum = "00";
        }
        StrlimtNum += QString::number(limtNum,16).toUpper();
        nowerrNum.append(StrlimtNum);
        //qDebug() << "fnowerrNum ="<<nowerrNum;
        i++;
    }
    if(i == 0)
    {
        emit  sendFaultInfoClear(1,false);
    }
    return i;
}

void FaultInfoDialog::fill_tableWidget_faultInfo()
{
    int i = 0, j = 0;
    char *faultStr1;
    char *faultStr3;

    for( i = 0; i < faultNum; i++ )
    {
        ba = tc->fromUnicode( faultCode[i] );faultStr1 = ba.data();
        ba = tc->fromUnicode( faultDescrip[i] );faultStr3 = ba.data();
        ui->tableWidget_faultInfo->item(i, 1)->setText(tr(faultStr1));
        ui->tableWidget_faultInfo->item(i, 2)->setText(tr(faultStr3));
        //ui->tableWidget_faultInfo->item(i, 1)->setFlags(Qt::ItemIsSelectable);//bukexuanzhong
        //ui->tableWidget_faultInfo->item(i, 1)->setSelected(true);
        //ui->tableWidget_faultInfo->item(i, 2)->setFlags(Qt::ItemIsSelectable);//bukexuanzhong
        //ui->tableWidget_faultInfo->item(i, 2)->setSelected(true);
    }
}

//向上
void FaultInfoDialog::on_Btn_up_clicked()
{
    if(listingrow == 0)
    {
        return;
    }
    listingrow = ui->tableWidget_faultInfo->verticalScrollBar()->value();
    ui->tableWidget_faultInfo->verticalScrollBar()->setSliderPosition(listingrow-1);
    listingrow = ui->tableWidget_faultInfo->verticalScrollBar()->value();
    set_btn_state();
}

//向下
void FaultInfoDialog::on_Btn_down_clicked()
{
    listingrow = ui->tableWidget_faultInfo->verticalScrollBar()->value();
    if(listingrow >= faultNum-5)
    {
        listingrow = faultNum-5;
    }
    ui->tableWidget_faultInfo->verticalScrollBar()->setSliderPosition(listingrow+1);
    listingrow = ui->tableWidget_faultInfo->verticalScrollBar()->value();
    set_btn_state();
}

void FaultInfoDialog::set_btn_state()
{
    if( faultNum == 0 )
    {
        ui->Btn_up->setEnabled(false);
        ui->Btn_down->setEnabled(false);
    }
    else if( faultNum > 0 )
    {
        if( faultNum <= 5 )
        {
            ui->Btn_up->setEnabled(false);
            ui->Btn_down->setEnabled(false);
        }
        else
        {
            if( listingrow == 0 )
            {
                ui->Btn_up->setEnabled(false);
                ui->Btn_down->setEnabled(true);
            }
            else if( listingrow > 0 && listingrow < faultNum-5 )
            {
                ui->Btn_up->setEnabled(true);
                ui->Btn_down->setEnabled(true);
            }
            else if( listingrow >= faultNum-5 )
            {
                ui->Btn_up->setEnabled(true);
                ui->Btn_down->setEnabled(false);
            }
        }
    }
}

void FaultInfoDialog::updatefaultInfo()
{
  if(!Btclearfaultflg)
  {
    tableWidget_creat();
  }
}

void FaultInfoDialog::faultInfo_pressedHandle(int row, int colunm)
{
    //printf("%d~~~~~row = %d\n", __LINE__, row);
    char *faultStr2;
    ui->faultInfo_textEdit->clear();
    ////qDebug()<<"faultState.count() = "<<faultState.count();
    if(faultState.count() > row)
    {
        ////qDebug()<<"row = "<<row;
        ba = tc->fromUnicode( faultState[row] );

        faultStr2 = ba.data();
        ui->faultInfo_textEdit->setText(tr(faultStr2));
        selectedIndex = row;
    }
    else
    {
        ////qDebug()<<"colunm = "<<colunm;
        if(ui->tableWidget_faultInfo->item(row,colunm)->isSelected())
        {
            for(int k = 0;k<3;k++)
            {
              ////qDebug()<<"k = "<<k;
              ui->tableWidget_faultInfo->item(row,k)->setSelected(false);
            }
            ui->tableWidget_faultInfo->item(row,colunm)->setFlags(Qt::ItemIsSelectable);
        }
    }
}

void FaultInfoDialog::on_Btn_faultDelete_clicked()
{
         QSqlQuery query;
         query.prepare(QObject::tr("delete from FaultinfTable"));
         query.exec();
         QTime n = QTime::currentTime();
         QTime now;
         do{

             now = QTime::currentTime();
         }
         while(n.msecsTo(now)<=10);
         timeisover = true;//时序是否正常完成标志
         isinitimessuccess = true;//初始化时序完成
         iscountover = true; //运算完成
         signaltimes = true;//单帧命令完成
         errorflg = true;//下次再按清洗针标志
         ledopenflg = 1;
         //isSystemSleep = false;
         ErrClearBtnflg =true;
         global_errdealflg =false;
         global_setbeepflg =false;
         int fd_sing = main_control_data.serialport->getffd();
         ioctl(fd_sing,SET_BEEP,0);
         Clear_RerrNum.clear();
         nowerrNum.clear();
         global_faultInfo.clear();
         global_faultlevel.clear();
         faultCode.clear();
         faultState.clear();
         faultDescrip.clear();
         faultdealorder.clear();
         Xsamexeflg = false;
         Ysamexeflg = false;
         Diffexeflg = false;
         Dilexeflg = false;

         communcheckflg = false;
         Xsamreturnflg = false;
         Ysamreturnflg = false;
         Diffreturnflg = false;
         Dilreturnflg = false;

         Dilreagentreturnflg = true;
         Lhreagentreturnflg = true;
         Diffreagentreturnflg = true;
         fluidreturnflg = false;

         XsamErrrepeat = 0;
         YsamErrrepeat = 0;
         DiffErrrepeat = 0;
         DilErrrepeat = 0;
         Communcheckrepeat = 0;

         Dilreagentrepeat =  0;
         Lhreagentrepeat =  0;
         Diffreagentrepeat =  0;

         negativeErrrepeat=  0;
         hydraulicpressurerepeat=  0;
         fluidErrrepeat =  0;
         Diltempalarmrepeat  =  0;
          Difftempalarmrepeat  =  0;
          Hgbalarmrepeat = 0;
          Vhalarmrepeat  = 0;

         ui->Btn_clearFault->setEnabled(false);
         ui->Btn_faultDelete->setEnabled(false);
         ui->Btn_faultInfoClose->setEnabled(true);
         Btclearfaultflg = false;
         tableWidget_creat();
         FalutSummaInfo =  tr("一键删除");
         FalutinfoFunc = tr("故障已全部删除");
         save_journal_info(FalutSummaInfo,FalutinfoFunc,1);
         main_control_data.faultshowflg = false;
         allowEnterSleep = oldfaultallowEnterSleep;
         allowMaintenanceExe = oldfaultallowMaintenanceExe;
         SystemSleepexitfalut = false;
         this->close();
}

void FaultInfoDialog::on_Btn_faultInfoClose_clicked()
{
    this->close();
}

void FaultInfoDialog::closeEvent(QCloseEvent *e)
{
    main_control_data.faultshowflg = false;
    allowEnterSleep = oldfaultallowEnterSleep;
    allowMaintenanceExe = oldfaultallowMaintenanceExe;
    timer->stop();
    timerreadmicroswitch->stop();
    timermicroswitchdeal->stop();
}

void FaultInfoDialog::showEvent(QShowEvent *e)
{
 //   qDebug()<<"showEvent faultinfodialog ";
//    timeisover =true;
//    signaltimes =true;
    oldfaultallowEnterSleep = allowEnterSleep;
    oldfaultallowMaintenanceExe = allowMaintenanceExe;//有先后顺序
    tableWidget_creat();

    allowEnterSleep = false;
    allowMaintenanceExe = false;
    ErrClearBtnflg =false;
    global_faultcode.clear();
    Xsamexeflg = false;
    Ysamexeflg = false;
    Diffexeflg = false;
    Dilexeflg = false;
    communcheckflg = false;

    Btclearfaultflg = false;
    Btclearfaultstep = 0;

    fluidreturnflg = false;

     XsamErrrepeat = 0;
     YsamErrrepeat = 0;
     DiffErrrepeat = 0;
     DilErrrepeat = 0;
     Communcheckrepeat = 0;

     Dilreagentrepeat =  0;
     Lhreagentrepeat =  0;
     Diffreagentrepeat =  0;

     negativeErrrepeat=  0;
     hydraulicpressurerepeat=  0;
     fluidErrrepeat =  0;

     Diltempalarmrepeat  =  0;
      Difftempalarmrepeat  =  0;
      Hgbalarmrepeat = 0;
      Vhalarmrepeat  = 0;
      if(falutflg)
      {
          ui->Btn_faultDelete_2->hide();
          ui->en_E_LineEdit->hide();
      }
    if(userDatalist.at(6).toInt()  ==  0)
    {
        ui->Btn_faultDelete->show();
    }
    else
    {
        ui->Btn_faultDelete->hide();
    }

    main_control_data.faultshowflg = true;
    isOpenResagent =false;

    QSqlQuery query;
    if(query.prepare("select * from SysParamTable where ID = :id") == false)
    {

        return;
    }
    query.bindValue(":id",1);
    query.exec();
    QSqlRecord rec = query.record();
    while(query.next())
    {
        rec = query.record();
        int inttemp;
        inttemp = rec.value(rec.indexOf("isOpenResagent")).toInt();
        isOpenResagent = (inttemp == 55);

    }
    if(isSystemSleep)
    {
        timermicroswitchdeal->start(10);
    }
    else
    {
        timermicroswitchdeal->stop();
    }
}
static void selfcheck_send(int flag)
{
    char hexbuf[20];
    int fd;


    fd = main_control_data.serialport->getmfd();

    if(flag == 0)//80X
    {
        hexbuf[0] = 0x68;
        hexbuf[1] = 0x2b;
        hexbuf[2] = 0x03;
        hexbuf[3] = 0x11;
        hexbuf[4] = 0x03;
        hexbuf[5] = (unsigned char)(10-1);
        hexbuf[6] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4] + hexbuf[5]) % 8;
        hexbuf[7] = 0x16;
        main_control_data.serialport->sendData(hexbuf, fd, 8 );
    }
    else if(flag == 1)//y
    {
        hexbuf[0] = 0x68;
        hexbuf[1] = 0x2b;
        hexbuf[2] = 0x05;
        hexbuf[3] = 0x51;
        hexbuf[4] = 0x13;
        hexbuf[5] = (unsigned char)(10 & 0xff);
        hexbuf[6] = 0x28;
        hexbuf[7] = 0x03;
        hexbuf[8] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4] + hexbuf[5] + hexbuf[6] + hexbuf[7]) % 8;
        hexbuf[9] = 0x16;
        main_control_data.serialport->sendData(hexbuf, fd, 10 );
    }
    else if(flag == 2)//X
    {
//        hexbuf[0] = 0x68;
//        hexbuf[1] = 0x2b;
//        hexbuf[2] = 0x05;
//        hexbuf[3] = 0x51;
//        hexbuf[4] = 0x13;
//        hexbuf[5] = (unsigned char)(10 & 0xff);
//        hexbuf[6] = 0x28;
//        hexbuf[7] = 0x03;
//        hexbuf[8] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4] + hexbuf[5] + hexbuf[6] + hexbuf[7]) % 8;
//        hexbuf[9] = 0x16;
//        main_control_data.serialport->sendData(hexbuf, fd, 10 );

//        QTime n = QTime::currentTime();
//        QTime now;
//        do{
//            now = QTime::currentTime();
//        }
//        while(n.msecsTo(now)<=100);


        hexbuf[0] = 0x68;
        hexbuf[1] = 0x2b;
        hexbuf[2] = 0x05;
        hexbuf[3] = 0x51;
        hexbuf[4] = 0x11;
        hexbuf[5] = (unsigned char)(5 & 0xff);
        hexbuf[6] = 0x28;
        hexbuf[7] = 0x03;
        hexbuf[8] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4] + hexbuf[5] + hexbuf[6] + hexbuf[7]) % 8;
        hexbuf[9] = 0x16;
        main_control_data.serialport->sendData(hexbuf, fd, 10 );
    }
    else if(flag == 3)
    {
        hexbuf[0] = 0x68;
        hexbuf[1] = 0x2b;
        hexbuf[2] = 0x05;
        hexbuf[3] = 0x23;
        hexbuf[4] = 0x20;
        hexbuf[5] = 0x0A;
        hexbuf[6] = 0x28;
        hexbuf[7] = 0x90;
        hexbuf[8] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4] + hexbuf[5] + hexbuf[6] + hexbuf[7]) % 8;
        hexbuf[9] = 0x16;
        main_control_data.serialport->sendData(hexbuf, fd, 10 );
    }
    else if(flag == 4)
    {
        hexbuf[0] = 0x68;
        hexbuf[1] = 0x2b;
        hexbuf[2] = 0x05;
        hexbuf[3] = 0x22;
        hexbuf[4] = 0x20;
        hexbuf[5] = 0x0A;
        hexbuf[6] = 0x28;
        hexbuf[7] = 0x90;
        hexbuf[8] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4] + hexbuf[5] + hexbuf[6] + hexbuf[7]) % 8;
        hexbuf[9] = 0x16;
        main_control_data.serialport->sendData(hexbuf, fd, 10 );
    }
    else if(flag == 5)//DIL TEMT 638 11
    {
        hexbuf[0] = 0x68;
        hexbuf[1] = 0x29;
        hexbuf[2] = 0x02;
        hexbuf[3] = 0x60;
        hexbuf[4] = 0x55;//0x57
        hexbuf[5] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4]) % 8;
        hexbuf[6] = 0x16;
        main_control_data.serialport->sendData(hexbuf,fd,7);
    }
    else if(flag == 6)//DIFF TEMT 633 12
    {
        hexbuf[0] = 0x68;
        hexbuf[1] = 0x29;
        hexbuf[2] = 0x02;
        hexbuf[3] = 0x60;
        hexbuf[4] = 0x57;//0x55
        hexbuf[5] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4]) % 8;
        hexbuf[6] = 0x16;
        main_control_data.serialport->sendData(hexbuf,fd,7);
    }
    else if(flag == 7)//废液桶已满 "50C","23"
    {
        hexbuf[0] = 0x68;
        hexbuf[1] = 0x29;
        hexbuf[2] = 0x02;
        hexbuf[3] = 0x60;
        hexbuf[4] = 0x54;
        hexbuf[5] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4]) % 8;
        hexbuf[6] = 0x16;
        main_control_data.serialport->sendData(hexbuf,fd,7);
    }
    else if(flag == 8)
    {
        char hexbuf[7];
        hexbuf[0] = 0x68;
        hexbuf[1] = 0x29;
        hexbuf[2] = 0x02;
        hexbuf[3] = 0x60;
        hexbuf[4] = 0x61;
        hexbuf[5] = (hexbuf[1] + hexbuf[2] + hexbuf[3] + hexbuf[4]) % 8;
        hexbuf[6] = 0x16;
        main_control_data.serialport->sendData(hexbuf,fd,7);
        QTime n = QTime::currentTime();
        QTime now;
        do{
            now = QTime::currentTime();
        }
        while(n.msecsTo(now)<=100);
    }
}

void FaultInfoDialog::on_Btn_clearFault_clicked()
{
    int fd;


    if(isSystemSleep)
    {
        //正在休眠
        //KMessageBox msgBox;
        msgBox.setTipInfo(tr("仪器正在休眠，请按吸样键退出。"));
        msgBox.hideBotton("Cancel");
        //msgBox.setShowTimes(6000);
        msgBox.showKMessageBox();
        return;
    }

    timeisover = true;//时序是否正常完成标志
    isinitimessuccess = true;//初始化时序完成
    iscountover = true; //运算完成
    signaltimes = true;//单帧命令完成

    ErrClearBtnflg =true;
    Btclearfaultflg =true;
    global_errdealflg =true;
    Clear_RerrNum.clear();

    fd = serialport->getmfd();


    Xsamexeflg = false;
    Ysamexeflg = false;
    Diffexeflg = false;
    Dilexeflg = false;

    communcheckflg = false;
    Xsamreturnflg = false;
    Ysamreturnflg = false;
    Diffreturnflg = false;
    Dilreturnflg = false;

    Dilreagentreturnflg = true;
    Lhreagentreturnflg = true;
    Diffreagentreturnflg = true;
    fluidreturnflg = false;

    XsamErrrepeat = 0;
    YsamErrrepeat = 0;
    DiffErrrepeat = 0;
    DilErrrepeat = 0;
    Communcheckrepeat = 0;

    Dilreagentrepeat =  0;
    Lhreagentrepeat =  0;
    Diffreagentrepeat =  0;

    negativeErrrepeat=  0;
    hydraulicpressurerepeat=  0;
    fluidErrrepeat =  0;
    Diltempalarmrepeat  =  0;
     Difftempalarmrepeat  =  0;
     Hgbalarmrepeat = 0;
     Vhalarmrepeat  = 0;
 #if 1
    ui->Btn_clearFault->setEnabled(false);
    ui->Btn_faultDelete->setEnabled(false);
    ui->Btn_faultInfoClose->setEnabled(false);
    FalutSummaInfo =  tr("一键消除");
    FalutinfoFunc = tr("启动一键消除");
    save_journal_info(FalutSummaInfo,FalutinfoFunc,1);
    clearFault_step();
#endif
}
void FaultInfoDialog::progressBarinit(int times)
{
    num = (times/10);
    posnum = 0;

    allowEnterSleep = false;
    allowMaintenanceExe = false;
    ui->progressBar->setValue(0);
    ui->progressBar->setRange(0,num);
    timer->start(500);
    ui->Btn_clearFault->setEnabled(false);
    ui->Btn_faultDelete->setEnabled(false);
    ui->Btn_faultInfoClose->setEnabled(false);
    //qDebug() <<"allowMaintenanceExe = "<<allowMaintenanceExe<<oldfaultallowMaintenanceExe;
}

void FaultInfoDialog::progressBarstop()
{
    timer->stop();
    ui->progressBar->setValue(posnum);
    DelayMsecTime(100);
    ui->progressBar->setValue(0);
    if((main_control_data.faultshowflg == true && reagentdialogwinshowflg != true) && (Btclearfaultstep == 6|| Btclearfaultstep == 7||Btclearfaultstep == 8))//true false
    {
       return;
    }
    else
    {
       timerreadmicroswitch->start(50);
    }



//    ui->Btn_clearFault->setEnabled(true);
//    ui->Btn_faultDelete->setEnabled(true);
//    ui->Btn_faultInfoClose->setEnabled(true);
}
void FaultInfoDialog::UpdateProgess()
{
    //qDebug() <<"allowMaintenanceExe = "<<allowMaintenanceExe<<oldfaultallowMaintenanceExe;
     //有先后顺序
    if((posnum++) < num)
    {
        ui->progressBar->setValue(posnum);
    }
    else
    {
        ui->progressBar->setValue(0);
        progressBarstop();
    }
}
void FaultInfoDialog::seccssdeal_fault()
{
        QString Itemfaultcode;
        QSqlQuery query;
        bool samerrflg;

        for(int i = 0;i<faultCode.count();i++)
        {
            Itemfaultcode = faultCode.at(i);
            samerrflg =true ;
            for(int j = 0;j<Clear_RerrNum.count();j++)//sam_err check
            {
                 if(Itemfaultcode == Clear_RerrNum.at(j))
                 {
                         samerrflg =false;
                         break;
                 }
            }
            if(samerrflg)
            {
                query.prepare(QObject::tr("delete from FaultinfTable where Faultcode = :code"));
                query.bindValue(":code",Itemfaultcode);
                query.exec();
            }
        }
        nowerrNum.clear();
        global_faultInfo.clear();
        global_faultlevel.clear();
        faultCode.clear();
        faultState.clear();
        faultDescrip.clear();
        faultdealorder.clear();
        tableWidget_creat();
}
void FaultInfoDialog::receivetimessuccessignals(int type,bool fun1,bool fun2)
{
    if(main_control_data.faultshowflg == true && reagentdialogwinshowflg == true)//true false
    {
       return;
    }
    //qDebug()<<"FaultInfoDialog  global_reagenDiffrpeflg global_reagenDilrpeflg global_reagenLhrpeflg ="<<global_reagenDiffrpeflg<<global_reagenDilrpeflg<<global_reagenLhrpeflg;
    //if((type == 42 && Btclearfaultstep == 8) || (type == 43&& Btclearfaultstep == 7) || (type == 44&& Btclearfaultstep == 6))//界面不显示，则不发送消息
    if((type == 42) || (type == 43) || (type == 44))//界面不显示，则不发送消息
    {
        bool TofalutChangeflg = true;
        //qDebug()<<"FaultInfoDialog  global_reagenDiffrpeflg global_reagenDilrpeflg global_reagenLhrpeflg ="<<global_reagenDiffrpeflg<<global_reagenDilrpeflg<<global_reagenLhrpeflg;
        if((fun1 == true)&&(fun2 == true))
        {
            int reagflg =0xFF;
            if(type == 42)
            {
              reagflg = 7;
              if( global_reagenDilrpeflg == 0x66)
              {
                  TofalutChangeflg  = false;
              }
            }
            else if(type == 43)
            {
               reagflg = 6;
               if( global_reagenDiffrpeflg == 0x66)
               {
                   TofalutChangeflg  = false;
               }
            }
            else if(type == 44)
            {
               reagflg = 5;
               //qDebug()<<"reagentcolseflg global_reagenLhrpeflg ="<<global_reagenLhrpeflg;
               if( global_reagenLhrpeflg == 0x66)
               {
                   TofalutChangeflg  = false;
               }
            }
        }
        else
        {
            TofalutChangeflg  = false;
        }
        //qDebug()<<"reagentcolseflg TofalutChangeflg ="<<TofalutChangeflg;
        //if((type == 42 && Btclearfaultstep == 8) || (type == 43&& Btclearfaultstep == 7) || (type == 44&& Btclearfaultstep == 6))//界面不显示，则不发送消息

        if((Btclearfaultstep == 8) ||(Btclearfaultstep == 7)||(Btclearfaultstep == 6))
        {
             reagentcolseflg(TofalutChangeflg);
        }
        else
        {
            if(type == 42)
            {
                if( global_reagenDilrpeflg == 0x66)
                {

                }
                else if( global_reagenDilrpeflg == 0xff)
                {
                    otherreagentdealflg(1);
                }
                global_reagenDilrpeflg = 0;
            }
            else if(type == 44)
            {
                if( global_reagenLhrpeflg == 0x66)
                {

                }
                else  if( global_reagenLhrpeflg == 0xff)
                {
                    otherreagentdealflg(2);
                }
                global_reagenLhrpeflg = 0;
            }
            else if(type == 43)
            {
                if( global_reagenDiffrpeflg == 0x66)
                {

                }
                else  if( global_reagenDiffrpeflg == 0xff)
                {
                    otherreagentdealflg(3);
                }
                global_reagenDiffrpeflg = 0;
            }
        }
    }

    if(this->isHidden() == true || main_control_data.faultshowflg == false || reagentdialogwinshowflg)//界面不显示，则不发送消息
    {
        return;
    }

    if((fun1 == true)&&(fun2 == true))
    {

    }
    else
    {
         ui->progressBar->setValue(0);
         progressBarstop();
         if((Btclearfaultstep == 101) ||(Btclearfaultstep == 102)||(Btclearfaultstep == 103))
         {
             SystemSleepexitfalut =true;//fail
         }

    }
}
void FaultInfoDialog::receiveallsignals(QString id, QStringList list)
{
    if(this->isHidden() == true)
    {
        return;
    }
    //qDebug()<<"receiveallsignals id="<<id;
    QString cmd = list.at(0);
    bool ok;
    int command = cmd.toInt(&ok,16);

    QString temp;
    int tempH = 0;
    int temper = 0;


    //qDebug()<<"command="<<command;
    if(command == 0x54)
    {
        ////qDebug()<<"list.at(1) ="<<list.at(1);
        if( list.at(1) == "0")
        {
            fluidreturnflg = true;
        }
        else
        {
            fluidreturnflg = false;
        }

    }
    else if(command == 0x61)
    {
        temp = list.at(1);
        tempH = temp.toInt(&ok,16);
        temper = tempH;
        temp = list.at(2);
        tempH = temp.toInt(&ok,16);
        temper = (temper<<8) + tempH;
        pressure[0] = (temper)/38.337 - 427.35+adjustVal[2];
        pressure[0] = int(pressure[0]*10)/10.0;

    }
    else if(command == 0x63)
    {
        temp = list.at(1);
        unsigned char tempH = temp.toInt(&ok,16);
        //qDebug()<<"tempH="<<tempH;
        int bit8 = tempH & 0x80;
        //qDebug()<<"bit8="<<bit8;
        if(bit8 == 0)
        {
            adjustVal[0] =tempH *(-0.1);
        }
        else
        {
            char tempL = tempH & 0x7F;
            adjustVal[0] =tempL *(0.1);
        }
    }
}

void FaultInfoDialog::receivesuccessignals(int type,bool signal)
{
    if(this->isHidden() == true)
    {
        return;
    }
    //qDebug()<<"FaultInfoDialog@@@@@@@@receivesuccessignals";
    switch (type)
    {
        case 0x5113://Y
        {
            if( true == signal)
            {
                Ysamreturnflg = true;
            }
            else
            {
                Ysamreturnflg = false;
            }
            Ysamexeflg = true;
        }
        break;
        case 0x5111://X
        {
            if( true == signal)
            {
                Xsamreturnflg = true;
            }
            else
            {
                Xsamreturnflg = false;
            }
            Xsamexeflg = true;
        }
        break;
        case 0x2220://DILL
        {
            if( true == signal)
            {
                Diffreturnflg = true;
            }
            else
            {
                Diffreturnflg = false;
            }
            Diffexeflg = true;
        }
        break;
        case 0x2320://DIL
        {
            if( true == signal)
            {
                Dilreturnflg = true;
            }
            else
            {
                Dilreturnflg = false;
            }
            Dilexeflg = true;
        }
        break;
        case 0x1103:
        {
            communcheckflg = true;
        }
        break;
        case 0x1112:
        {

        }
        break;
        case 0x6054://feiye
        {

        }
        break;
        default: break;
    }
}

void FaultInfoDialog::changreaget_dialogshow(char checkStatus)
{
    QString Functiontype;
    QString Statustype;
    if(checkStatus == 0)
    {
        Functiontype = "0";
        global_reagenDiffrpeflg = 0xff;
    }
    else if(checkStatus == 1)
    {
        Functiontype = "1";
        global_reagenDilrpeflg  = 0xff;
    }
    else if(checkStatus == 2)
    {
        Functiontype = "2";
        global_reagenLhrpeflg = 0xff;
    }
    emit sendreagetsamNum(Functiontype);
//    reagentInfodialog->show();
     sendshowdialog("reagentinfodialog",Functiontype);
     reagentdialogwinshowflg = true;
    timerreadmicroswitch->stop();
}
void FaultInfoDialog::Faultto_clearFault()
{
    bool ok;
    bool startcomeflg = false;
    FalutinfoFunc = "";
    if(global_modechangfaultflg == true)//模式切换失败限制标志
    {
        SearchBinFile searchfile;
        QString str;
        int times;
        if(searchfile.search("F011",str,times))
         {
            global_modechangfaultflg = false;
            qDebug() <<"global_modechangfaultflg false";
            ui->Btn_clearFault->setEnabled(false);
            ui->Btn_faultDelete->setEnabled(false);
            ui->Btn_faultInfoClose->setEnabled(false);
            int funcTime = GetFuncTime(str);
            serialport->sendData(str,serialport->getmfd(),11,times);
            Btclearfaultstep = 620;
            timerreadmicroswitch->stop();
            progressBarinit(funcTime);
            return;
         }
    }
    communcheckflg = false;
    Btclearfaultflg =false;
    reset_exeoverflg = 0;
    global_errdealflg =false;

    ui->Btn_clearFault->setEnabled(true);
    ui->Btn_faultDelete->setEnabled(true);
    ui->Btn_faultInfoClose->setEnabled(true);
    timerreadmicroswitch->stop();
    timer->stop();
    ui->progressBar->setValue(0);
    tableWidget_creat();
    if(Btclearfaultstep == 601 && faultNum == 0)
    {
        FalutSummaInfo =  tr("一键消除");
        FalutinfoFunc = tr("一键消除结束");
         errorflg = true;//下次再按清洗针标志
         ledopenflg = 1;
         this->close();
          allowEnterSleep = oldfaultallowEnterSleep;
          allowMaintenanceExe = oldfaultallowMaintenanceExe;
         main_control_data.faultshowflg = false;
    }
    else
    {
        if(faultdealorder.count() == 1)
        {
           int value = faultdealorder.at(0).toInt(&ok,10);
           if(ok == true)
           {
               if(value == 25)//开机流程未执行成功
               {
                   Faultsccussdeal_save_journal(25);
                   tableWidget_creat();
                   FalutSummaInfo =  tr("一键消除");
                   FalutinfoFunc = tr("一键消除结束");
                   //跳出开机继续
                   startcomeflg = true;
                   errorflg = true;//下次再按清洗针标志
                   ledopenflg = 1;
               }
               else
               {
                   FalutSummaInfo =  tr("一键消除");
                   FalutinfoFunc = tr("一键消除结束，故障未能全部消除");
                   ledopenflg = 5;
               }
           }
        }
        else
        {
            FalutSummaInfo =  tr("一键消除");
            FalutinfoFunc = tr("一键消除结束，故障未能全部消除");
            ledopenflg = 5;
        }
    }
    save_journal_info(FalutSummaInfo,FalutinfoFunc,1);
    Btclearfaultstep = 0;
    if(startcomeflg)
    {
//        logindialog->start_process("fault");
//        logindialog->show();
        this->close();
        sendshowdialog("logindialog","fault");
    }
}

void FaultInfoDialog::clearFault_step()
{
    //SearchBinFile *searchfile = new SearchBinFile;
    SearchBinFile searchfile;
    QString str;
    QString str2;
    int times = 0;
    int fd;
    int i = 0;


    ErrClearBtnflg =true;
    Clear_RerrNum.clear();

    fd = serialport->getmfd();

    tableWidget_creat();
    //faultNum = get_faultInfo_from_db();
    Xsamexeflg = false;
    Ysamexeflg = false;
    Diffexeflg = false;
    Dilexeflg = false;
    communcheckflg = false;


    Xsamreturnflg = false;
    Ysamreturnflg = false;
    Diffreturnflg = false;
    Dilreturnflg = false;
    allowEnterSleep = false;
    allowMaintenanceExe = false;

    bool ok;
    if(Btclearfaultstep == 0)
    {
            int value;



            for(i = 0;i<faultdealorder.count();i++)
            {
                value = faultdealorder.at(i).toInt(&ok,10);
                ////qDebug() << "ok = " << ok;
                //Q_ASSERT(ok == false);
                if(ok == true)
                {
                    if(value == 27 )
                    {
                        if(hardware_V_check(0))
                        {
                            ui->Btn_clearFault->setEnabled(false);
                            ui->Btn_faultDelete->setEnabled(false);
                            ui->Btn_faultInfoClose->setEnabled(false);
                            Faultsccussdeal_save_journal(27);
                        }
                        else
                        {
                            Faultto_clearFault();
                            return;
                        }
                    }
                    else if(value == 28 )
                    {
                        if(hardware_V_check(1))
                        {
                            ui->Btn_clearFault->setEnabled(false);
                            ui->Btn_faultDelete->setEnabled(false);
                            ui->Btn_faultInfoClose->setEnabled(false);
                            Faultsccussdeal_save_journal(28);
                        }
                        else
                        {
                            Faultto_clearFault();
                            return;
                        }
                    }
                    else if(value == 29 )
                    {
                        if(hardware_V_check(2))
                        {
                            ui->Btn_clearFault->setEnabled(false);
                            ui->Btn_faultDelete->setEnabled(false);
                            ui->Btn_faultInfoClose->setEnabled(false);
                            Faultsccussdeal_save_journal(29);
                        }
                        else
                        {
                            Faultto_clearFault();
                            return;
                        }
                    }
                    else if(value == 30 )
                    {
                        if(hardware_V_check(3))
                        {
                            ui->Btn_clearFault->setEnabled(false);
                            ui->Btn_faultDelete->setEnabled(false);
                            ui->Btn_faultInfoClose->setEnabled(false);
                            Faultsccussdeal_save_journal(30);
                        }
                        else
                        {
                            Faultto_clearFault();
                            return;
                        }
                    }
                    else if(value == 31 )
                    {
                        if(hardware_V_check(4))
                        {
                            ui->Btn_clearFault->setEnabled(false);
                            ui->Btn_faultDelete->setEnabled(false);
                            ui->Btn_faultInfoClose->setEnabled(false);
                            Faultsccussdeal_save_journal(31);
                        }
                        else
                        {
                            Faultto_clearFault();
                            return;
                        }
                    }
                    else if(value == 32 )
                    {
                        if(hardware_V_check(5))
                        {
                            ui->Btn_clearFault->setEnabled(false);
                            ui->Btn_faultDelete->setEnabled(false);
                            ui->Btn_faultInfoClose->setEnabled(false);
                            Faultsccussdeal_save_journal(32);
                        }
                        else
                        {
                            Faultto_clearFault();
                            return;
                        }
                    }
                    else if(value == 13 )//记录仪故障
                    {
                        RecorderPrint recorderprint;
                        ui->Btn_clearFault->setEnabled(false);
                        ui->Btn_faultDelete->setEnabled(false);
                        ui->Btn_faultInfoClose->setEnabled(false);
                        int states = recorderprint.record_get_states();
                        if(states != -1)
                        {
                            Faultsccussdeal_save_journal(13);
                        }
                    }
                    else if(value == 14 )//记录仪缺纸
                    {
                        RecorderPrint recorderprint;
                        ui->Btn_clearFault->setEnabled(false);
                        ui->Btn_faultDelete->setEnabled(false);
                        ui->Btn_faultInfoClose->setEnabled(false);
                        int states = recorderprint.record_get_states();
                        if(states == 0)//-2
                        {
                            Faultsccussdeal_save_journal(14);
                        }
                    }
                    else if(value == 15 )//打印机故障
                    {
                        ui->Btn_clearFault->setEnabled(false);
                        ui->Btn_faultDelete->setEnabled(false);
                        ui->Btn_faultInfoClose->setEnabled(false);
                        Faultsccussdeal_save_journal(15);
                    }
                }
            }






            for(i = 0;i<faultdealorder.count();i++)
            {
                value = faultdealorder.at(i).toInt(&ok,10);
                ////qDebug() << "ok = " << ok;
                //Q_ASSERT(ok == false);
                if(ok == true)
                {
                    if(value == 8 )//一键消除
                    {
                        ui->Btn_clearFault->setEnabled(false);
                        ui->Btn_faultDelete->setEnabled(false);
                        ui->Btn_faultInfoClose->setEnabled(false);
                        Faultsccussdeal_save_journal(8);
                        tableWidget_creat();
                    }
                    if(value == 26 )//一键消除,可在其它界面消除，所有另起消除号.
                    {
                        ui->Btn_clearFault->setEnabled(false);
                        ui->Btn_faultDelete->setEnabled(false);
                        ui->Btn_faultInfoClose->setEnabled(false);
                        Faultsccussdeal_save_journal(26);
                        tableWidget_creat();
                    }

                    if(value == 0)
                    {
                        communcheckflg = false;
                        Btclearfaultstep = 1;
                        //qDebug() <<"allowMaintenanceExe3 = "<<allowMaintenanceExe<<oldfaultallowMaintenanceExe;
                        selfcheck_send(0);
                        timerreadmicroswitch->start(500);
                        ui->Btn_clearFault->setEnabled(false);
                        ui->Btn_faultDelete->setEnabled(false);
                        ui->Btn_faultInfoClose->setEnabled(false);
                        return;
                    }
                }
            }
    }

    int value;
    for(i = 0;i<faultdealorder.count();i++)
    {
            value = faultdealorder.at(i).toInt(&ok,10);
            if(ok == true)
            {
                if(value == 23 && fluidErrrepeat !=0xff)//废液
                {
                    fluidreturnflg = false;
                    selfcheck_send(7);
                    Btclearfaultstep = 9;
                    timerreadmicroswitch->start(1000);
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    return;
                }
                if(value == 1 && YsamErrrepeat !=0xff)
                {
                    Ysamreturnflg = false;
                    selfcheck_send(1);
                    Btclearfaultstep = 2;
                    timerreadmicroswitch->start(2000);
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    return;
                }

                if(value == 2 && YsamErrrepeat !=0xff && XsamErrrepeat !=0xff)
                {
                    if(YsamErrrepeat ==0)
                    {
                        Ysamreturnflg = false;
                        selfcheck_send(1);
                        Btclearfaultstep = 2;
                        timerreadmicroswitch->start(2000);
                        ui->Btn_clearFault->setEnabled(false);
                        ui->Btn_faultDelete->setEnabled(false);
                        ui->Btn_faultInfoClose->setEnabled(false);
                        return;
                    }
                    Xsamreturnflg = false;
                    selfcheck_send(2);
                    Btclearfaultstep = 3;
                    timerreadmicroswitch->start(4000);
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    return;
                }
                if(value == 3 && DilErrrepeat !=0xff)
                {
                    Dilreturnflg = false;
                    selfcheck_send(3);
                    Btclearfaultstep = 4;
                    timerreadmicroswitch->start(4000);
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    return;
                }
                if(value == 4 && DiffErrrepeat !=0xff)
                {
                    Diffreturnflg = false;
                    selfcheck_send(4);
                    Btclearfaultstep = 5;
                    timerreadmicroswitch->start(4000);
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    return;
              }
            }
            faultNum = get_faultInfo_from_db();
            if(faultNum == 0 && Btclearfaultstep > 1 &&fluidErrrepeat != 0xff)
            {
                ui->Btn_clearFault->setEnabled(false);
                ui->Btn_faultDelete->setEnabled(false);
                ui->Btn_faultInfoClose->setEnabled(false);
                searchfile.search("F007",str,times);
                int funcTime = GetFuncTime(str);
                int sendlength = serialport->sendData(str,fd,7,times);
                Btclearfaultstep = 10;
                timerreadmicroswitch->stop();
                progressBarinit(funcTime);
                return;
            }
    }

    for(i = 0;i<faultdealorder.count();i++)
    {
        value = faultdealorder.at(i).toInt(&ok,10);
        if(ok == true)
        {

            if(0 <= value && value <= 4)
            {
                Faultto_clearFault();
                return;
            }
            if(value == 9 && negativeErrrepeat == 0 )//50E
            {
                ui->Btn_clearFault->setEnabled(false);
                ui->Btn_faultDelete->setEnabled(false);
                ui->Btn_faultInfoClose->setEnabled(false);
                negativeErrrepeat = 0xFF;
//                QSqlQuery query;
//                query.prepare(QObject::tr("delete from FaultinfTable where dealnum = :dealNum"));
//                query.bindValue(":dealNum",9);
//                query.exec();
                if(!searchfile.search("F048",str,times))
                {
                    Faultto_clearFault();
                    return;
                }
                int funcTime = GetFuncTime(str);
                int sendlength = serialport->sendData(str,fd,48,times);
                //qDebug() << sendlength;

                progressBarinit(funcTime);
                Btclearfaultstep = 50;
                timerreadmicroswitch->stop();
                return;
            }
         }
    }
    //qDebug() <<"Dilreagentreturnflg 57 ";
    if(negativeErrrepeat == 0xff || Communcheckrepeat == 0xff ||XsamErrrepeat == 0xff ||YsamErrrepeat == 0xff ||DiffErrrepeat == 0xff ||DilErrrepeat == 0xff ||fluidErrrepeat == 0xff)
    {
        Faultto_clearFault();
        return;
    }
    else
    {
        if(faultNum != 0 &&reset_exeoverflg != 1&& (negativeErrrepeat == 0x66 ||Communcheckrepeat == 0x66 || XsamErrrepeat == 0x66 || YsamErrrepeat == 0x66 ||DiffErrrepeat == 0x66 ||DilErrrepeat == 0x66))
        {
            reset_exeoverflg = 1;
            ui->Btn_clearFault->setEnabled(false);
            ui->Btn_faultDelete->setEnabled(false);
            ui->Btn_faultInfoClose->setEnabled(false);
            searchfile.search("F007",str,times);
            int funcTime = GetFuncTime(str);
            int sendlength = serialport->sendData(str,fd,7,times);
            Btclearfaultstep = 10;
            timerreadmicroswitch->stop();
            progressBarinit(funcTime);
            return;
        }
    }

    //qDebug() <<"Dilreagentreturnflg 58 ";
    tableWidget_creat();
    timerreadmicroswitch->stop();
    clearFault_step_familytwo();
}
void FaultInfoDialog::clearFault_step_familytwo()
{
    SearchBinFile searchfile;
    QString str;
    int times = 0;
    int fd;
    int i=0;
    bool ok;
     fd = serialport->getmfd();

        tableWidget_creat();
         int value;
        for(i = 0;i<faultdealorder.count();i++)
        {
            value = faultdealorder.at(i).toInt(&ok,10);
            if(ok == true)
            {
                if(0 <= value && value <= 4)
                {
                    Faultto_clearFault();
                    //qDebug() <<"Dilreagentreturnflg 5 ";
                    return;
                }
                if((value == 7 || value == 34 || value ==37  )&& Dilreagentrepeat == 0 )//DIL
                {
                    Dilreagentreturnflg = false;
                    Btclearfaultstep = 8;
                    Dilreagentrepeat = 0xFF;
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    if(run_Ftime_check("F042"))
                    {
                           Faultto_clearFault();
                            return;
                    }
//                    if(!isOpenResagent)
//                    {
                        if(value == 7)
                        {
                            bool Reagentflg = true;
                            int otherval;
                            for(int j = 0;j<faultdealorder.count();j++)
                            {
                                otherval = faultdealorder.at(j).toInt(&ok,10);
                                if(ok == true)
                                {
                                    if(otherval == 34)//DIL
                                    {
                                       Reagentflg = false;
                                    }
                                }
                            }
                            if(Reagentflg)
                            breplaceReagent(0);
                            else
                            changreaget_dialogshow(0);
                        }
                        else
                        {
                            changreaget_dialogshow(0);
                        }
                        timerreadmicroswitch->stop();
//                    }
//                    else
//                    {
//                        timerreadmicroswitch->stop();
//                        //progressBarinit(funcTime);
//                        breplaceReagent(0);
//                    }
                    //qDebug() <<"Dilreagentreturnflg 6 ";
                    return;
                }
            }
        }
        for(i = 0;i<faultdealorder.count();i++)
        {
            value = faultdealorder.at(i).toInt(&ok,10);
            if(ok == true)
            {

//                if(0 <= value && value <= 7)
//                {
//                    Faultto_clearFault();
//                    return;
//                }
                if(value == 10 && hydraulicpressurerepeat == 0)//100
                {
                    selfcheck_send(8);

                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    hydraulicpressurerepeat = 0xFF;
                    global_pressurewarnrpeflg = 0xFF;

                    Btclearfaultstep = 300;
                    timerreadmicroswitch->stop();
                    QTime n = QTime::currentTime();
                    QTime now;
                    do{
                        now = QTime::currentTime();
                    }
                    while(n.msecsTo(now)<=200);
                    timerreadmicroswitch->start();
//                    global_errdealflg = false;

//                    if(!searchfile.search("F036",str,times))
//                    {
//                           //SystemSleepexitfalut =true;
//                           global_errdealflg = true;
//                           Faultto_clearFault();
//                           return;
//                    }
//                    global_errdealflg = true;
//                    int funcTime = GetFuncTime(str);
//                    int sendlength = serialport->sendData(str,fd,36,times);
//                    //qDebug() << sendlength;

//                    progressBarinit(funcTime);
//                    Btclearfaultstep = 300;
//                    timerreadmicroswitch->stop();
                    return;
                }
        }
     }

        if(SystemSleepexitfalut)//由于试剂问题退出休眠失败标志
        {


             SystemSleepexitfalut =false;
             Btclearfaultstep = 101;
             timerreadmicroswitch->start(50);
             return;
        }


    Btclearfaultstep = 0;
    tableWidget_creat();
    timerreadmicroswitch->stop();
    clearFault_step_familythree();
    //qDebug() <<"Dilreagentreturnflg 12 ";
}
void FaultInfoDialog::clearFault_step_familythree()
{
//    SearchBinFile searchfile;
//    QString str;
//    int times = 0;
//    int fd;
    int i = 0;
    bool ok;
    int value;

    ErrClearBtnflg =true;

//    fd = serialport->getmfd();

        tableWidget_creat();

        for(i = 0;i<faultdealorder.count();i++)
        {
            value = faultdealorder.at(i).toInt(&ok,10);
            if(ok == true)
            {
                if(0 <= value && value <= 4)
                {
                    Faultto_clearFault();
                    //qDebug() <<"Dilreagentreturnflg 5 ";
                    return;
                }
                if((value == 5 || value == 36 || value == 39)&& Lhreagentrepeat == 0&& Dilreagentrepeat != 0xFF )//LH
                {
                    Lhreagentreturnflg = false;
                    Btclearfaultstep = 6;
                    Lhreagentrepeat= 0xFF;
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    if(run_Ftime_check("F044"))
                    {
                           Faultto_clearFault();
                            return;
                    }
//                    if(!isOpenResagent)
//                    {
                        if(value == 5)
                        {
                            bool Reagentflg = true;
                            int otherval;
                            for(int j = 0;j<faultdealorder.count();j++)
                            {
                                otherval = faultdealorder.at(j).toInt(&ok,10);
                                if(ok == true)
                                {
                                    if(otherval == 36)//LH
                                    {
                                       Reagentflg = false;
                                    }
                                }
                            }
                            if(Reagentflg)
                            breplaceReagent(2);
                            else
                            changreaget_dialogshow(2);
                        }
                        else
                        {
                            changreaget_dialogshow(2);
                        }
                        timerreadmicroswitch->stop();
//                    }
//                    else
//                    {
//                        timerreadmicroswitch->stop();
//                         //progressBarinit(funcTime);
//                        int funcTime = breplaceReagent(2);
//                    }

                    //qDebug() <<"Dilreagentreturnflg 7 ";
                    return;
                }
                if((value == 6 || value == 35 || value == 38)&& Diffreagentrepeat == 0 && Dilreagentrepeat != 0xFF ) //DIff
                {
                    Diffreagentreturnflg = false;
                    Btclearfaultstep = 7;
                    Diffreagentrepeat= 0xFF;
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    if(run_Ftime_check("F043"))
                    {
                           Faultto_clearFault();
                            return;
                    }
//                    if(!isOpenResagent)
//                    {
                        if(value == 6)
                        {
                            bool Reagentflg = true;
                            int otherval;
                            for(int j = 0;j<faultdealorder.count();j++)
                            {
                                otherval = faultdealorder.at(j).toInt(&ok,10);
                                if(ok == true)
                                {
                                    if(otherval == 35)//DIff
                                    {
                                       Reagentflg = false;
                                    }
                                }
                            }
                            if(Reagentflg)
                            breplaceReagent(1);
                            else
                            changreaget_dialogshow(1);
                        }
                        else
                        {
                            changreaget_dialogshow(1);
                        }
                        timerreadmicroswitch->stop();
//                    }
//                    else
//                    {
//                        timerreadmicroswitch->stop();
//                         //progressBarinit(funcTime);
//                        int funcTime = breplaceReagent(1);
//                    }
                    return;
                }
            }
        }
        //qDebug() <<"Dilreagentreturnflg 11 ";



    Btclearfaultstep = 0;
    timerreadmicroswitch->stop();
    tableWidget_creat();
    clearFault_step_familyfour();
}
void FaultInfoDialog::clearFault_step_familyfour()
{
    SearchBinFile searchfile;
    QString str;
    QString str2;

    int times = 0;
    int fd;
    int i = 0;
    bool ok;
    int value;

    ErrClearBtnflg =true;

    fd = serialport->getmfd();
        for(i = 0;i<faultdealorder.count();i++)
        {
            value = faultdealorder.at(i).toInt(&ok,10);
            if(ok == true)
            {
                //qDebug() <<"value read OK! "<<value;
                if(value == 11 && Diltempalarmrepeat  ==  0)//dil temp
                {
                    global_faultcode.clear();
                    selfcheck_send(5);
                    Diltempalarmrepeat  =  0xff;
                    Btclearfaultstep = 380;
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    timerreadmicroswitch->start(3000);
                    return;
                }

                if(value == 12 && Difftempalarmrepeat  ==  0)//diff temp
                {
                    global_faultcode.clear();
                    selfcheck_send(6);
                    Difftempalarmrepeat  =  0xff;
                    Btclearfaultstep = 381;
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    timerreadmicroswitch->start(3000);
                    return;
                }
            }
        }

        for(i = 0;i<faultdealorder.count();i++)
        {
            value = faultdealorder.at(i).toInt(&ok,10);
            if(ok == true)
            {
                //qDebug() <<"value read OK! "<<value;
                if(value == 33 &&  Hgbalarmrepeat == 0)//HGB本底电压异常
                {
                    Btclearfaultstep = 382;
                    ui->Btn_clearFault->setEnabled(false);
                    ui->Btn_faultDelete->setEnabled(false);
                    ui->Btn_faultInfoClose->setEnabled(false);
                    if(!searchfile.search("F032",str,times))
                    {
                        Faultto_clearFault();
                        return;
                    }
                    int funcTime = GetFuncTime(str);
                    int sendlength = serialport->sendData(str,fd,32,times);
                    progressBarinit(funcTime);
                    timerreadmicroswitch->stop();
                    return;
                }
            }
        }


        for(i = 0;i<faultdealorder.count();i++)
        {
            value = faultdealorder.at(i).toInt(&ok,10);
            if(ok == true)
            {

                if((0 <= value && value <= 4 )|| (9 == value)|| (10 == value))
                {
                    Faultto_clearFault();
                    //qDebug() <<"Dilreagentreturnflg 5 ";
                    return;
                }
            }
        }


        if((Vhalarmrepeat  == 0x66 || hydraulicpressurerepeat == 0x66 || negativeErrrepeat == 0x66 || Communcheckrepeat == 0x66 || XsamErrrepeat == 0x66 || YsamErrrepeat == 0x66 ||DiffErrrepeat == 0x66 ||DilErrrepeat == 0x66))
        {
            qDebug() <<"come 0x66  = "<<Vhalarmrepeat<<hydraulicpressurerepeat<<negativeErrrepeat <<Communcheckrepeat <<XsamErrrepeat<<YsamErrrepeat <<DiffErrrepeat <<DilErrrepeat;
            ui->Btn_clearFault->setEnabled(false);
            ui->Btn_faultDelete->setEnabled(false);
            ui->Btn_faultInfoClose->setEnabled(false);
            searchfile.search("F007",str,times);
            int funcTime = GetFuncTime(str);
            //searchfile.search("F029",str2,times);
            //funcTime += GetFuncTime(str2);
            int sendlength = serialport->sendData(str,fd,7,times);
            //sendlength = serialport->sendData(str2,fd,29,times);
            timerreadmicroswitch->stop();
            progressBarinit(funcTime);
            XsamErrrepeat =0;
            YsamErrrepeat =0;
            DiffErrrepeat = 0 ;
            DilErrrepeat = 0;
            hydraulicpressurerepeat = 0;
            Communcheckrepeat = 0;
            negativeErrrepeat = 0;
            Btclearfaultstep = 400;
            Vhalarmrepeat = 0;
            //qDebug() <<"Dilreagentreturnflg 23 ";
            return;
        }
        else
        {
            Btclearfaultstep = 601;
            Faultto_clearFault();
            //qDebug() <<"Dilreagentreturnflg 22 ";
        }
}
bool FaultInfoDialog::FaultOperateManage()
{
    if(this->isHidden() == true)//界面不显示，则不发送消息
    {
        return true;
    }
    if(Btclearfaultflg)
    {
        ui->progressBar->setValue(0);
        progressBarstop();
        Faultto_clearFault();
    }
    //if(reagentInfodialog->isHidden() == false)//界面不显示，则不发送消息
    {
        emit sendtimeerrtorg();
    }
    return true;
}
void FaultInfoDialog::on_Btn_faultDelete_2_clicked()
{
     QString falutcode = ui->en_E_LineEdit->text().toUpper();

     if(!falutflg)
     {
         if(!Btclearfaultflg)
         {
     //         timeisover = true;//时序是否正常完成标志
              isinitimessuccess = true;//初始化时序完成
              iscountover = true; //运算完成
              signaltimes = true;//单帧命令完成

              ErrClearBtnflg =true;
              Btclearfaultflg =true;
              global_errdealflg =true;
              Clear_RerrNum.clear();


              Xsamexeflg = false;
              Ysamexeflg = false;
              Diffexeflg = false;
              Dilexeflg = false;

              communcheckflg = false;
              Xsamreturnflg = false;
              Ysamreturnflg = false;
              Diffreturnflg = false;
              Dilreturnflg = false;

              Dilreagentreturnflg = true;
              Lhreagentreturnflg = true;
              Diffreagentreturnflg = true;
              fluidreturnflg = false;

              XsamErrrepeat = 0;
              YsamErrrepeat = 0;
              DiffErrrepeat = 0;
              DilErrrepeat = 0;
              Communcheckrepeat = 0;

              Dilreagentrepeat =  0;
              Lhreagentrepeat =  0;
              Diffreagentrepeat =  0;

              negativeErrrepeat=  0;
              hydraulicpressurerepeat=  0;
              fluidErrrepeat =  0;
              Diltempalarmrepeat  =  0;
              Difftempalarmrepeat  =  0;
              main_control_data.faultshowflg = false;
              timerreadmicroswitch->start(100);
              Btclearfaultstep = 0xFFFF;
              allowEnterSleep = oldfaultallowEnterSleep;
              allowMaintenanceExe = oldfaultallowMaintenanceExe;
              this->close();
              falutcodelist.clear();
              falutcodelist.append(falutcode);
              if( falutcode == "8201")
              {
                  SystemSleepexitfalut = true;
              }
              global_errdealflg =false;
         }
         else
         {
             falutcodelist.clear();
             falutcodelist.append(falutcode);
             if( falutcode == "8201")
             {
                 SystemSleepexitfalut = true;
             }
         }

     }
     else
     {
         timerreadmicroswitch->stop();
         Btclearfaultstep = 0;
         emit sendfaultcode("faultcode",falutcodelist);
     }
}
bool FaultInfoDialog::otherreagentdealflg(int reagentnum)
{
    if(reagentnum  == 1)//DIL
    {
        Faultsccussdeal_save_journal(7);
//        Faultsccussdeal_save_journal(34);
//        Faultsccussdeal_save_journal(37);
        get_faultInfo_from_db();
    }
    else if(reagentnum  == 2 )//LH
    {
        Faultsccussdeal_save_journal(5);
//        Faultsccussdeal_save_journal(36);
//        Faultsccussdeal_save_journal(39);
        get_faultInfo_from_db();
    }
    else if(reagentnum  == 3 ) //DIff
    {
        Faultsccussdeal_save_journal(6);
//        Faultsccussdeal_save_journal(35);
//        Faultsccussdeal_save_journal(38);
        get_faultInfo_from_db();
    }
    else if(reagentnum  == 4)//DIL
    {
        Faultsccussdeal_save_journal(34);
        Faultsccussdeal_save_journal(37);
        get_faultInfo_from_db();
    }
    else if(reagentnum  == 5 )//LH
    {
        Faultsccussdeal_save_journal(36);
        Faultsccussdeal_save_journal(39);
        get_faultInfo_from_db();
    }
    else if(reagentnum  == 6 ) //DIff
    {
        Faultsccussdeal_save_journal(35);
        Faultsccussdeal_save_journal(38);
        get_faultInfo_from_db();
    }
    else if(reagentnum  == 7)//DIL
    {
        Faultsccussdeal_save_journal(37);
        get_faultInfo_from_db();
    }
    else if(reagentnum  == 8 )//LH
    {
        Faultsccussdeal_save_journal(39);
        get_faultInfo_from_db();
    }
    else if(reagentnum  == 9 ) //DIff
    {
        Faultsccussdeal_save_journal(38);
        get_faultInfo_from_db();
    }
}

bool FaultInfoDialog::wintoclearfault(int faultnum) // 提供给主界面用于消除记录仪故障的接口
{
    qDebug() << "recorderprintcome states2=  "<<faultnum;

    if(faultnum  == 2)//clock   // 记录仪故障
    {
        Faultsccussdeal_save_journal(13);
        get_faultInfo_from_db();
    }
    else if(faultnum  == 3)//clock  // 记录仪打开，或者记录仪缺纸
    {
        Faultsccussdeal_save_journal(14);
        get_faultInfo_from_db();
    }
    else if(faultnum == 1) //打印机故障
    {
        Faultsccussdeal_save_journal(15);
        get_faultInfo_from_db();
    }
}
bool FaultInfoDialog::otherclearfault(int faultnum)
{
    if(faultnum  == 1)//clock
    {
        Faultsccussdeal_save_journal(26);
        get_faultInfo_from_db();
    }
}
bool FaultInfoDialog::reagentcolseflg(bool flg)
{
    bool ok;
    qDebug() <<"Dilreagentreturnflg 9 "<<flg;
    if(this->isHidden() == true)//界面不显示，则不发送消息
    {
        return 0;
    }

    if(flg)
    {
        if(Btclearfaultstep  == 8)//DIL
        {
            Faultsccussdeal_save_journal(7);
            Faultsccussdeal_save_journal(34);
            Faultsccussdeal_save_journal(37);
            tableWidget_creat();
             int i,value;
            for(i = 0;i<faultdealorder.count();i++)
            {
                value = faultdealorder.at(i).toInt(&ok,10);
                if(ok == true)
                {
                    if((0 <= value && value <= 4 ))
                    {
                        global_reagenDilrpeflg = 0;
                        global_reagenDiffrpeflg = 0;
                        global_reagenLhrpeflg = 0;
                        reagentdialogwinshowflg = false;
                        //Faultto_clearFault();
                        //return 0;
                    }
                }
            }
        }
        if(Btclearfaultstep  == 6 )//LH
        {
            Faultsccussdeal_save_journal(5);
            Faultsccussdeal_save_journal(36);
            Faultsccussdeal_save_journal(39);
            tableWidget_creat();
             int i,value;
            for(i = 0;i<faultdealorder.count();i++)
            {
                value = faultdealorder.at(i).toInt(&ok,10);
                if(ok == true)
                {
                    if((0 <= value && value <= 4 )|| value == 7)
                    {
                        global_reagenDilrpeflg = 0;
                        global_reagenDiffrpeflg = 0;
                        global_reagenLhrpeflg = 0;
                        reagentdialogwinshowflg = false;
                        //Faultto_clearFault();
                        //return 0;
                    }
                }
            }
        }
        if(Btclearfaultstep  == 7 ) //DIff
        {
            Faultsccussdeal_save_journal(6);
            Faultsccussdeal_save_journal(35);
            Faultsccussdeal_save_journal(38);
            tableWidget_creat();
            int i,value;
            for(i = 0;i<faultdealorder.count();i++)
            {
                value = faultdealorder.at(i).toInt(&ok,10);
                if(ok == true)
                {
                    if((0 <= value && value <= 4 )|| value == 7)
                    {
                        global_reagenDilrpeflg = 0;
                        global_reagenDiffrpeflg = 0;
                        global_reagenLhrpeflg = 0;
                        reagentdialogwinshowflg = false;
                        //Faultto_clearFault();
                        //return 0;
                    }
                }
            }
        }
        global_reagenDilrpeflg = 0;
        global_reagenDiffrpeflg = 0;
        global_reagenLhrpeflg = 0;
        reagentdialogwinshowflg = false;
      timerreadmicroswitch->start(50);
    }
    else
    {
        global_reagenDilrpeflg = 0;
        global_reagenDiffrpeflg = 0;
        global_reagenLhrpeflg = 0;
        reagentdialogwinshowflg = false;
        Faultto_clearFault();
    }

}
int FaultInfoDialog::breplaceReagent(int ReagentName)
{

    QString Functiontype;
    int funcTime;
    QString str;
    int times = 0;
    SearchBinFile searchfile;

    if(ReagentName == 0)//DIL
    {
        global_reagenDilrpeflg = 0xff;//监测是否重新出现试剂报警
        Functiontype = "F042";
        searchfile.search(Functiontype,str,times);
        funcTime = GetFuncTime(str);
    }
    else if(ReagentName == 1)//DIFF
    {
        global_reagenDiffrpeflg = 0xff;//监测是否重新出现试剂报警
        Functiontype = "F043";
        searchfile.search(Functiontype,str,times);
        funcTime = GetFuncTime(str);
    }
    else if(ReagentName == 2)//LH
    {
        global_reagenLhrpeflg = 0xff;//监测是否重新出现试剂报警
        Functiontype = "F044";
        searchfile.search(Functiontype,str,times);
        funcTime = GetFuncTime(str);
    }
    progressBarinit(funcTime);
    int m_mfd = serialport->getmfd();
    int Functionnumtype = Functiontype.mid(1,3).toInt();
    serialport->sendData(str,m_mfd,Functionnumtype,times);
    return funcTime;
}

void FaultInfoDialog::Showfaultdialog()
{
    isOpenResagent =false;
    reagentdialogwinshowflg = false;

    QSqlQuery query;
    if(query.prepare("select * from SysParamTable where ID = :id") == false)
    {
        return;
    }
    query.bindValue(":id",1);
    query.exec();
    QSqlRecord rec = query.record();
    while(query.next())
    {
        rec = query.record();
        int inttemp;
        inttemp = rec.value(rec.indexOf("isOpenResagent")).toInt();
        isOpenResagent = (inttemp == 55);
    }

    this->update();
    this->show();//exec
}
bool  FaultInfoDialog::hardware_V_check(int vcheckflg )
{

       unsigned short temp_data;
       float voltage;
       bool clearvfault = true;
       int dev_fd = main_control_data.serialport->getffd();

//       ioctl(dev_fd, GET_RH_ORG_DATA, &temp_data);
//       voltage[1] = temp_data/4096.00*5;//功率板+24V (激光电压)

       if(vcheckflg == 0)
       {
           ioctl(dev_fd, GET_VCONST_ORG_DATA, &temp_data);
           voltage = temp_data/4096.00*5*41.1;//恒流源电压
           if(voltage<55||voltage>65)
           {
               clearvfault = false;
           }
       }
       else if(vcheckflg == 1)
       {
           ioctl(dev_fd, GET_UVCC_ORG_DATA, &temp_data);
           voltage = temp_data/4096.00*5*6;//模拟+12V
           if(voltage<11||voltage>14)
           {
               clearvfault = false;
           }
       }
       else if(vcheckflg == 2)
       {
           ioctl(dev_fd, GET_DVCC_ORG_DATA, &temp_data);
           voltage = -temp_data/4096.00*5*4.98;//模拟-12V
           if(voltage<-14||voltage>-11)
           {
               clearvfault = false;
           }
       }
       else if(vcheckflg == 3)
       {
          clearvfault = false;
       }
       else if(vcheckflg == 4)
       {
            clearvfault = false;
       }
       else if(vcheckflg == 5)
       {
            clearvfault = false;
       }
       else if(vcheckflg == 6)
       {
            clearvfault = false;
       }
       else if(vcheckflg == 7)
       {
            clearvfault = false;
       }
       else if(vcheckflg == 8)
       {
            clearvfault = false;
       }
       else if(vcheckflg == 9)
       {
           ioctl(dev_fd, GET_HGB_ORG_DATA, &temp_data);
           voltage = temp_data/4096.00*5;//HGB电压
           if(voltage<3.8||voltage>4.8)
           {
               clearvfault = false;
           }
       }
       if(clearvfault == true)
       {
          Vhalarmrepeat  = 0x66;
       }
       else
       {
          Vhalarmrepeat  = 0;
       }
       return clearvfault;
}
void FaultInfoDialog::ifuncreadmicroswitch()
{
    if(this->isHidden() == true || main_control_data.faultshowflg == false)//界面不显示，则不发送消息
    {
        return;
    }
    //qDebug()<<"FaultInfoDialog::ifuncreadmicroswitch come ";
    if(isSystemSleep)
    {
         emit sendreadreadmicroswitch("");
    }
    else
    {
         timermicroswitchdeal->stop();
         msgBox.close();
    }
}
